home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / Sound / SimpleSounds.java.z / SimpleSounds.java
Encoding:
Java Source  |  2003-08-08  |  10.8 KB  |  288 lines

  1. /*
  2.  *    @(#)SimpleSounds.java 1.38 02/10/21 13:54:28
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.net.URL;
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import com.sun.j3d.utils.applet.MainFrame;
  45. import com.sun.j3d.utils.geometry.ColorCube;
  46. import com.sun.j3d.utils.universe.*;
  47. import java.io.File;
  48. import java.security.*;
  49. import javax.media.j3d.*;
  50. import javax.vecmath.*;
  51.  
  52. /*
  53.  * This Java3D program:
  54.  *    Creates an instance of the JavaSoundMixer AudioDevice, initializing it
  55.  *         and attaching it to the PhysicalEnvironment by using
  56.  *         SimpleUniverse.
  57.  *    Creates one uncached Background and two cached Point sound sources.
  58.  *    Creates and executes a custom behavior (SimpleSoundsBehavior) that
  59.  *         starts the sound playing and transforms the PointSound source
  60.  *         by modifying the TransformGroup that contains the Sound nodes.
  61.  *
  62.  * Usage: 
  63.  *    java SimpleSounds [URLpath [name1 [name2 [name2]]]]
  64.  *
  65.  * The first optional command line parameter is the URL path to directory
  66.  *       containing "file:" or "http:" and then directory path string.
  67.  *    If you are using the suppled default sound files in the same directory
  68.  *       as this test program then only the URLpath need be supplied on the 
  69.  *       command line.
  70.  *    If this parameter is not included then the current path to the directory
  71.  *       this program is running in is used for an application
  72.  *       and the codebase is used for an applet.
  73.  * The second thru fourth optional command line parameters are sound file names
  74.  *    If not given, the default file names are:
  75.  *       techno_machine.au
  76.  *       hello_universe.au
  77.  *       roar.au
  78.  *    that correspond to the 3 'voice' quality, 8-bit, u-law, 8-kHz samples
  79.  *    included in the same directory as this test program.
  80.  * 
  81.  *    Java Sound engine has been advertised to support the following 8- and 16-
  82.  *    bit, linear and u-law, mono and stereo sound sample file formats: AU,
  83.  *    AIFF, WAV, and PCM.  Non-cached MIDI and RMF files are also supported.
  84.  *    Neither compressed formats (DVI, GSM, MOD) nor A-law formated files are
  85.  *    supported at this time.
  86.  */
  87.  
  88. public class SimpleSounds extends Applet {
  89.  
  90.     // File name of sound sample
  91.     private static  int filenamesGiven = 0;
  92.     private static URL[] url = new URL[3];
  93.     private static String[] filename = new String[3];
  94.     private static String path = null;
  95.     private static boolean filenamesSet = false;
  96.  
  97.     private SimpleUniverse u = null;
  98.  
  99.     public BranchGroup createSceneGraph() {
  100.     // Create the root of the subgraph
  101.     BranchGroup objRoot = new BranchGroup();
  102.  
  103.     // Create the transform group node and initialize it to the identity.
  104.     // Enable the TRANSFORM_WRITE capability so that our behavior code
  105.     // can modify it at runtime.  Add it to the root of the subgraph.
  106.     TransformGroup objTrans = new TransformGroup();
  107.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  108.     objRoot.addChild(objTrans);
  109.  
  110.     // Create a simple shape leaf node and add it into the scene graph.
  111.     objTrans.addChild(new ColorCube(0.4));
  112.  
  113.     // Create a new Behavior object that will perform the desired
  114.     // operation on the specified transform object and add it into the
  115.     // scene graph.
  116.     Transform3D yAxis = new Transform3D();
  117.         Alpha rotation = new Alpha(-1, Alpha.INCREASING_ENABLE,
  118.                                    0, 0,
  119.                                    20000, 0, 0,
  120.                                    0, 0, 0);
  121.         RotationInterpolator rotator =
  122.             new RotationInterpolator(rotation,
  123.                                      objTrans, yAxis,
  124.                                      0.0f, (float) Math.PI*2.0f);
  125.     BoundingSphere bounds =
  126.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  127.     rotator.setSchedulingBounds(bounds);
  128.     objTrans.addChild(rotator);
  129.         /*
  130.          * Create a sound node and add it to the scene graph
  131.          */
  132.         BackgroundSound sound1 = new BackgroundSound();
  133.         PointSound sound2 = new PointSound();
  134.         PointSound sound3 = new PointSound();
  135.         sound1.setCapability(PointSound.ALLOW_ENABLE_WRITE);
  136.         sound1.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
  137.         sound1.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
  138.         sound1.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
  139.         sound1.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
  140.         sound1.setCapability(PointSound.ALLOW_RELEASE_WRITE);
  141.         sound1.setCapability(PointSound.ALLOW_DURATION_READ);
  142.         sound1.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
  143.         sound1.setCapability(PointSound.ALLOW_LOOP_WRITE);
  144.         sound2.setCapability(PointSound.ALLOW_ENABLE_WRITE);
  145.         sound2.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
  146.         sound2.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
  147.         sound2.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
  148.         sound2.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
  149.         sound2.setCapability(PointSound.ALLOW_RELEASE_WRITE);
  150.         sound2.setCapability(PointSound.ALLOW_DURATION_READ);
  151.         sound2.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
  152.         sound2.setCapability(PointSound.ALLOW_POSITION_WRITE);
  153.         sound2.setCapability(PointSound.ALLOW_LOOP_WRITE);
  154.         sound3.setCapability(PointSound.ALLOW_ENABLE_WRITE);
  155.         sound3.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
  156.         sound3.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
  157.         sound3.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
  158.         sound3.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
  159.         sound3.setCapability(PointSound.ALLOW_RELEASE_WRITE);
  160.         sound3.setCapability(PointSound.ALLOW_DURATION_READ);
  161.         sound3.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
  162.         sound3.setCapability(PointSound.ALLOW_POSITION_WRITE);
  163.  
  164.     BoundingSphere soundBounds =
  165.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  166.         sound1.setSchedulingBounds(soundBounds);
  167.         sound2.setSchedulingBounds(soundBounds);
  168.         sound3.setSchedulingBounds(soundBounds);
  169.     objTrans.addChild(sound1);
  170.     objTrans.addChild(sound2);
  171.     objTrans.addChild(sound3);
  172.  
  173.  
  174.         /*
  175.      * Create a new Behavior object that will play the sound
  176.      */
  177.     SimpleSoundsBehavior player = new SimpleSoundsBehavior(
  178.                   sound1, sound2, sound3, 
  179.                   url[0], url[1], url[2], soundBounds);
  180.     player.setSchedulingBounds(soundBounds);
  181.     objTrans.addChild(player);
  182.  
  183.         // Let Java 3D perform optimizations on this scene graph.
  184.         objRoot.compile();
  185.  
  186.     return objRoot;
  187.     }
  188.  
  189.     public SimpleSounds() {
  190.     }
  191.  
  192.     public void init() {
  193.         if (!filenamesSet) {
  194.             // path is null if started from appletviewer/browser 
  195.         if (path == null) {
  196.             // the path for an applet
  197.             path = getCodeBase().toString();
  198.             }
  199.             int j;
  200.             /*
  201.              * append given file name to given URL path
  202.              */
  203.             for (j=0; j<filenamesGiven; j++)
  204.             filename[j] = new String(path + "/" + filename[j]);
  205.             /*
  206.              * fill in default file names if all three not given
  207.              */
  208.             for (int i=j; i<3; i++) {
  209.                 if (i == 0)
  210.             filename[0] = new String(path + "/techno_machine.au");
  211.                 if (i == 1)
  212.             filename[1] = new String(path + "/hello_universe.au");
  213.                 if (i == 2)
  214.             filename[2] = new String(path + "/roar.au");
  215.             }
  216.             filenamesSet = true;
  217.         }
  218.  
  219.     setLayout(new BorderLayout());
  220.         GraphicsConfiguration config =
  221.            SimpleUniverse.getPreferredConfiguration();
  222.  
  223.         Canvas3D c = new Canvas3D(config);
  224.     add("Center", c);
  225.  
  226.         /*
  227.          * Change filenames into URLs
  228.          */
  229.         for (int i=0; i<3; i++) {
  230.             try {
  231.                 url[i] = new URL(filename[i]);
  232.             }
  233.             catch (Exception e) {
  234.             System.out.println(e.getMessage());
  235.                 return;
  236.             }
  237.         }
  238.  
  239.     /*
  240.          * Create a simple scene and attach it to the virtual universe
  241.          */
  242.     u = new SimpleUniverse(c);
  243.         AudioDevice audioDev = u.getViewer().createAudioDevice();
  244.     BranchGroup scene = createSceneGraph();
  245.  
  246.         // This will move the ViewPlatform back a bit so the
  247.         // objects in the scene can be viewed.
  248.         u.getViewingPlatform().setNominalViewingTransform();
  249.  
  250.     u.addBranchGraph(scene);
  251.     }
  252.  
  253.     public void destroy() {
  254.     u.cleanup();
  255.     }
  256.  
  257.     /*
  258.      * The following allows SimpleSounds to be run as an application
  259.      * as well as an applet
  260.      */
  261.     public static void main(String[] args) {
  262.         if (args.length > 0) {
  263.             if ( (args[0].startsWith("file"+File.pathSeparator)) || 
  264.                  (args[0].startsWith("http"+File.pathSeparator))   ) {
  265.                 path = args[0];
  266.             }
  267.             else {
  268.             path = "file:" + args[0];
  269.             }
  270.     }
  271.     else {
  272.         path = "file:.";
  273.     }  
  274.     
  275.         for (int i=0; i<3; i++) {
  276.             if (args.length > (i+1)) {
  277.                 filename[i] = args[i+1];
  278.                 if (filename[i] != null) {
  279.                     filenamesGiven++ ;
  280.                 }
  281.             }
  282.             else
  283.                 break;
  284.         }
  285.     new MainFrame(new SimpleSounds(), args, 256, 256);
  286.     }
  287. }
  288.